<?php

$mcrp = './';
include_once('functions/session.php');
include_once('functions/nanofight.php');

$head = '<link href="nano/nano.css" rel="stylesheet" media="screen" type="text/css" title="style" />
<script type="text/javascript" src="nano/nanoajax.js"></script>
<script type="text/javascript" src="nano/nanofight.js"></script>';
include($mcrp . 'skin/default.php');
include($mcrp . 'skin/header.php');

if(isset($_POST['comment']) && $_POST['comment'] != '' && isset($_POST['id']) && is_numeric($_POST['id']) && (int)$_POST['id'] == $_POST['id'])
{
	AddFightComment($_POST['id'], $_POST['comment']);
}
if(isset($_GET['fight']) && is_numeric($_GET['fight']) && intval($_GET['fight']) == $_GET['fight'])
{
	NanoRegisterPlayer((int)$_GET['fight'], $mcuser->id);
	header('Location: nanofight.php');
	exit;
}
else if(isset($_GET['confirm']) && is_numeric($_GET['confirm']) && intval($_GET['confirm']) == $_GET['confirm'])
{
	NanoConfirmRegisterPlayer((int)$_GET['confirm'], $mcuser->id);
	header('Location: nanofight.php');
	exit;
}
else if(isset($_GET['decline']) && is_numeric($_GET['decline']) && intval($_GET['decline']) == $_GET['decline'])
{
	NanoDeclineRegisterPlayer((int)$_GET['decline'], $mcuser->id);
	header('Location: nanofight.php');
	exit;
}
else if(isset($_GET['del']) && $mcuser->isadmin)
{
	NanoDeleteFight($_GET['del']);
}

$fights = GetAllFightsByStatut();

?>
<div class="mc_box">
	<h3><a href="nano.php">Nanocontest</a> :: Combats</h3>
	<?php if($mcuser->id == 0) { ?>
	<div><center><img src="img/stop-trackback.png"><br/>Vous devez être connecté pour accéder à cette page.<br />Si vous n'êtes pas encore inscrit, rendez-vous sur la page d'<a href="register.php">inscription</a>.</center></div>
	<?php } else { ?>
	<div class="mc_box_content">
	<div style="width:95%;margin:auto;">Note : les combats effectués ne sont pas pris en compte dans le décompte des points et le classement pour l'instant. Les résultats peuvent être supprimés à tout moment.</div>
	<table id="nc_fight_list" class="mc_table">
		<tr>
			<th><a class="nc_button nc_bb" style="float:left;margin:-2px;" href="nanocreate.php">Nouveau</a>Joueurs</th>
			<th>Options</th>
		</tr>
		<?php
		$statutorder = array('ready', 'new', 'done');
		foreach($statutorder as $statut)
		{
			if(!is_array($fights[$statut]) || empty($fights[$statut])) continue;
			switch($statut)
			{
			case 'ready':  $statuttext = 'Combats en cours'; break;
			case 'new': $statuttext = 'Combats en attente'; break;
			case 'done': $statuttext = 'Combats terminés'; break;
			}
			echo '<tr><td colspan=3 class="nc_fight_statut">' . $statuttext . '</td></tr>';
						
			foreach($fights[$statut] as $f)
			{
				$bLaunch = true;
				?>
				<tr><td>
					<div class="nc_fight_user"><?php echo $f->id; ?></div>
				<?php
				$ispart = array_key_exists($mcuser->id, $f->players);
				foreach($f->players as $p)
				{
					$name = $p[0]->name . '<span>' . (($p[0]->nanodefeat+$p[0]->nanowin)?100*round($p[0]->nanowin/($p[0]->nanodefeat+$p[0]->nanowin),2):0) . '% - ' . $p[0]->nanowin . ' victoire' . ($p[0]->nanwin>=2?'s':'') . ' - ' . $p[0]->nanodefeat . ' défaite' . ($p[0]->nanodefeat>=2?'s':'') . '</span>';
					if($statut == 'done' /**/&& isset($p[2])/**/) echo '<div class="nc_fight_user nc_fight_me' . ($p[1] == 'winner'?' nc_fight_winner':'') . '">' . $name . '</div><div class="nc_fight_win">' . round($p[2]/11,2)*100 . '%</div>';
					else if($p[0]->id == 0) echo '<div class="nc_fight_user ' . ($ispart?'nc_fight_empty">En attente':'nc_fight_free" onclick="NanoRegister(' . $f->id . ');">S\'inscrire') . '</div>';
					else if($p[1] == 'wait' && $p[0]->id == $mcuser->id) echo '<a onclick="NanoDeclineRegister(' . $f->id . ');" class="nc_button_left nc_fight_decline">X</a><div class="nc_fight_user nc_fight_waitme">' . $name . '</div><div onclick="NanoConfirmRegister(' . $f->id . ');" class="nc_button_right nc_fight_accept">V</div>';
					else if($statut == 'ready' && $p[0]->id == $mcuser->id) echo '<div class="nc_fight_user nc_fight_me">' . $name . '</div><a href="nanomanage.php?id=' . $f->id . '" class="nc_button_right">Gérer</a>';
					else if($p[1] == 'winner') echo '<div class="nc_fight_user nc_fight_winner">' . $name . '</div>';
					else echo '<div class="nc_fight_user' . ($p[1] == 'wait'?' nc_fight_waiting':'') . '">' . $name . '</div>';
					if($p[1] != 'prgm') $bLaunch = false;
				}
				?>
				</td>
				<td>
				<?php
				if($statut == 'done' || ($bLaunch && $ispart)) { $comment = GetFightComment($f->id); ?><a href="nanovm.php?exec=<?php echo $f->id; ?>" class="nc_button_left">Visionner</a><a href="#" onclick="ToggleDisplay('nc_comment_<?php echo $f->id; ?>');return false;" class="nc_button_right">(<?php echo $comment['count']; ?>)</a><?php }
				if($mcuser->isadmin) { ?><span style="float:right;"><a class="nc_button" href="?del=<?php echo $f->id; ?>">X</a></span>
				<?php } ?>
				</td></tr>
				<?php if($statut == 'done') { ?><tr id="nc_comment_<?php echo $f->id; ?>" class="nc_comment" style="display:none;"><td colspan=3><div class="nc_comment_div"><form method="POST"><input name="comment" style="width:80%;color:#555;" value="Ajouter un commentaire" onclick="this.value='';this.style.color='';"><button style="float:right;" type="submit" class="nc_button">Ajouter</button><input name="id" type="hidden" value="<?php echo $f->id; ?>"></form></div><div class="nc_comment_div"><?php echo $comment['comment']; ?></div></td></tr><?php } ?>
			<?php
			}
		}
		?>
	</table>
	</div>
	<?php } ?>
</div>
<?php

include($mcrp . 'skin/footer.php');

?>